#include "golfdds.h"
#include <pthread.h>
void msgcallback(void *data, int len) {
  printf("alpha recv msg from beta:%s\n", (char *)data);
}

void *publishthread(void *param) {
  GOLFDDS_PUBLISHER_HANDLE publisher = (GOLFDDS_PUBLISHER_HANDLE)param;
  while (1) {
    if (golfdds_subscriber_count(publisher) > 0) {
      // printf("has %d subscribers,will publish\n", golfdds_subscriber_count(publisher));
      const char hellomsg[32] = "myhello,world from alpha!";
      golfdds_publish(publisher, hellomsg);
    } else {
      // printf("no subscriber,just sleep!\n");
      usleep(10 * 1000);
    }
  }
  return 0;
}
int main(int argc, const char *argv[]) {
  GOLFDDS_NODE_HANDLE node = golfdds_create_node("alpha");
  GOLFDDS_PUBLISHER_HANDLE publisher = golfdds_create_publisher(node, "alphatopic", 32, 4); // 创建一个发布者，主题名称为puretopic,主题消息大小32字节，消息队列4
  GOLFDDS_SUBSCRIBER_HANDLE subscriber = golfdds_create_subscriber(node, "betatopic", msgcallback);

  pthread_t threadid;
  pthread_create(&threadid, NULL, publishthread, publisher);
  golfdds_spin(node);

  return 0;
}